package net.sf.google.talk.eclipse.views;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;

public class OnlineFilter extends ViewerFilter {
	private final Roster roster;

	public OnlineFilter(final Roster roster) {
		super();
		if (roster == null) {
			throw new IllegalArgumentException("roster is null");
		}
		this.roster = roster;
	}

	@Override
	public boolean select(final Viewer viewer, final Object parentElement, final Object element) {
		//		System.out.println("element: "+element);
		if (element instanceof RosterEntry) {
			final RosterEntry e = (RosterEntry) element;
			final String user = e.getUser();
			if (user == null) {
				return false;
			}
			final Presence p = roster.getPresence(user);
			System.out.println("Presence: " + p);
			if (p != null && p.getType() != null && p.getType().equals(Type.available)) {
				return true;
			}
		}
		return false;
	}

}
